"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const defaults_1 = require("../defaults");
const source_root_helpers_1 = require("../../utils/source-root.helpers");
function main(options) {
    options = transform(options);
    return schematics_1.mergeWith(generate(options));
}
exports.main = main;
function transform(options) {
    const target = Object.assign({}, options);
    target.author = !!target.author ? target.author : defaults_1.DEFAULT_AUTHOR;
    target.description = !!target.description
        ? target.description
        : defaults_1.DEFAULT_DESCRIPTION;
    target.language = !!target.language ? target.language : defaults_1.DEFAULT_LANGUAGE;
    target.name = core_1.strings.dasherize(target.name);
    target.version = !!target.version ? target.version : defaults_1.DEFAULT_VERSION;
    target.cpaasVersion = !!target.cpaasVersion
        ? target.cpaasVersion
        : defaults_1.DEFAULT_CPAAS_VERSION;
    const setObj = {};
    if (target.set) {
        if (typeof target.set === 'string') {
            target.set.split(',').forEach((kv) => {
                const [envName, envValue] = kv.split('=');
                if (envName && envValue) {
                    setObj[envName] = envValue;
                }
            });
        }
    }
    target.set = setObj;
    target.demoName = !!target.demoName ? target.demoName : defaults_1.DEFAULT_DEMO_NAME;
    target.cpaasPackages = !!target.cpaasPackages ? target.cpaasPackages : '';
    target.cpaasFrontVersion = !!target.cpaasFrontVersion
        ? target.cpaasFrontVersion
        : defaults_1.DEFAULT_CPAAS_FRONT_VERSION;
    target.cpaasCliVersion = !!target.cpaasCliVersion
        ? target.cpaasCliVersion
        : require('cpaas-cli/package.json').version;
    target.packageManager = !!target.packageManager
        ? target.packageManager
        : 'npm';
    target.dependencies = !!target.dependencies ? target.dependencies : '';
    target.devDependencies = !!target.devDependencies
        ? target.devDependencies
        : '';
    return target;
}
function addNpmrcRegistry(registry) {
    return (host) => {
        let file = './.npmrc.template';
        if (host.exists(file)) {
            let content = host.read(file).toString() || '';
            if (!content.includes('registry=')) {
                content =
                    `registry=${registry ||
                        'http://nexus.coraise.cn:8000/repository/group/'}\n` + content;
                host.overwrite(file, content);
            }
        }
        return host;
    };
}
exports.addNpmrcRegistry = addNpmrcRegistry;
function generate(options) {
    return schematics_1.apply(schematics_1.url(core_1.join('./files', options.language)), [
        schematics_1.template(Object.assign({}, core_1.strings, options)),
        addNpmrcRegistry(options.registry),
        schematics_1.move(options.name),
        source_root_helpers_1.removeTemplateExt(),
    ]);
}
